package xq.gwt.ui.crud;


import xq.gwt.mvc.model.AbstractEntityModel;
import xq.gwt.mvc.model.BooleanPropertyModel;
import xq.gwt.mvc.model.CommandModel;
import xq.gwt.mvc.model.EntityArrayPropertyModel;
import xq.gwt.mvc.model.EntityModel;
import xq.gwt.mvc.model.PropertyModel;
import xq.gwt.ui.widgets.entity.EntitySelectedListener;

public class EntitySelectorModel extends AbstractEntityModel implements EntitySelectedListener{
	
	private static final long serialVersionUID = 2470877128599951092L;
	protected EntityArrayPropertyModel entities;
	protected EntityModel selectedEntity;
	protected PropertyModel[] props;
	protected CommandModel[] commands;
	private BooleanPropertyModel okEnabled = new BooleanPropertyModel();
	private EntitySelectedListener selectionListener;
	private CancelListener cancelListener;
	
	public EntitySelectorModel(EntitySelectedListener selectionListener, CancelListener cancelListener){
		this.selectionListener = selectionListener;
		this.cancelListener = cancelListener;
		init();
	}
	
	public EntitySelectorModel(){
		init();
	}
	
	private void init(){
		entities = new EntityArrayPropertyModel();
		entities.setName("RECORDS");
		entities.setReadonly(true);
		updateOkEnabled();
		props = new PropertyModel[]{entities};
		commands = new CommandModel[]{new OkCommand(), new CancelCommand()};
	}
	
	@Override
	public String getTitle() {		
		return "Избор на запис";
	}

	@Override
	public PropertyModel[] getProperties() {		
		return props;
	}

	@Override
	public CommandModel[] getCommands() {		
		return commands;
	}
	
	
	
	public EntityArrayPropertyModel getEntities() {
		return entities;
	}

	

	public EntityModel getSelectedEntity() {
		return selectedEntity;
	}

	@Override
	public void entitySelected(EntityModel selectedEntity) {
		this.selectedEntity = selectedEntity;
		updateOkEnabled();
	}

	private void updateOkEnabled(){
		okEnabled.setValue(selectedEntity != null);
	}
	
	private void notifySelectionListener(){
		if(selectionListener != null){
			selectionListener.entitySelected(selectedEntity);
		}
	}
	
	private void notifyCancelListener(){
		if(cancelListener != null){
			cancelListener.cancel();
		}
	}

	private class OkCommand implements CommandModel{		
		private static final long serialVersionUID = -7997111170840423433L;
		

		public OkCommand(){
			
		}
		
		@Override
		public void execute() throws Exception {			
			notifySelectionListener();		
		}

		@Override
		public String getCaption() {			
			return "Ok";
		}

		@Override
		public String getName() {
			
			return "OK_COMMAND";
		}

		@Override
		public BooleanPropertyModel isEnabled() {			
			return okEnabled;
		}

		@Override
		public String getFeedback() {			
			return null;
		}		
	}
	
	private class CancelCommand implements CommandModel{		
		private static final long serialVersionUID = -7997111170840423433L;
		private BooleanPropertyModel enabled;

		public CancelCommand(){
			enabled = new BooleanPropertyModel();
			enabled.setValue(true);
		}
		
		@Override
		public void execute() throws Exception {
			entitySelected(null);
			notifyCancelListener();			
		}

		@Override
		public String getCaption() {			
			return "Cancel";
		}

		@Override
		public String getName() {
			
			return "CANCEL_COMMAND";
		}

		@Override
		public BooleanPropertyModel isEnabled() {			
			return enabled;
		}

		@Override
		public String getFeedback() {			
			return null;
		}		
	}
	
	
	public interface CancelListener {
		public void cancel();
	}


	@Override
	public EntityModel newInstance() {		
		return new EntitySelectorModel();
	}
}
